/**
*@author Kenneth WU
*@version 18/2/2012
//********************************************
// Boundary class for getting input 
// OOD --> Player Mode Window
// Also for printing the status of player
// *************************************
*@author Ayaz Maroof
*@version 23 Feb 2012
*@Modification: Changed the type of printSinglePlayerInfo to boolean
// *************************************
*@author Ming
*@version 11 Mar 2012
*@Modification: Add diceForRole, showRepeat, printOrder and pickRole function
// *************************************
*@author Syrings
*@version 31 Mar 2012
*@Modification: updateSEValue: call PlayerDataProcessor to update the SE value and display the updated status
// **************************************
*@author Ming 
*@version 11 Apr 2012
*@Modification: Changes: Modified getInput() for exception handling of player input
// **************************************/

package player;

import java.util.Scanner;
import card.MagicCard;

/** Boundary class for getting input */
public class PlayerIO {
	
	/** function that get user input Number of player*/
	public int inputNumOfPlayer() {		
		int numOfPlayer = 0;
		boolean validInput = false;
		
		do {
			/** Starting the game 
			// Ask player to input how many players*/
			Scanner n = new Scanner(System.in);
			
			System.out.println("How many players in total?");
			System.out.println("Limit from 2 to 6 only! Type in now: ");
			
			if (n.hasNextInt()) {
				numOfPlayer = n.nextInt();
				
				if (validPlayerNo(numOfPlayer))
					validInput = true;
				else
					numOfPlayer = 0;
			}
		
			if(!validInput)
				System.out.println("Wrong Input, try again!\n");
			
		} while(!validInput);
		
		return numOfPlayer;
	}
	
	public boolean validPlayerNo(int numOfPlayer){
		if(numOfPlayer <2 || numOfPlayer > 6) return false;
		else return true;
	}

	/** a method that printout a array of Player info*/
	public void printPlayerArr(Player[] playerArr) {
		System.out.println("\nThere are " + playerArr.length + " players joining the game now.");
		System.out.println("Below are their own information: ");
		System.out.println("--------------------------------------");
		
		for(int i=0; i<playerArr.length; i++) {
			boolean result = printSinglePlayerInfo(playerArr[i]);
			if (result)
				System.out.println("\nEnd of Player Information!!!!\n***************************************");
		}
	}
	
	/** a method to print out the player informationW*/
	public boolean printSinglePlayerInfo(Player p) {	
		MagicCard mCard = new MagicCard();
		int noOfMagicCards = 0;
		for(int i = 0; i<3; i++){
			if(p.getMagicCard(i))
				noOfMagicCards++;
		}
		
		System.out.println("Player " + p.getPlayerID() + "\tRole: " + p.getRole());
		System.out.println("Player " + p.getPlayerID() +" right now has " + p.getSEPoint() + " SE Points");
		System.out.println("Player " + p.getPlayerID() + " now has " + noOfMagicCards + " magic card");
		for(int i=0; i<3;i++){
			if(p.getMagicCard(i))
				System.out.println(mCard.getName(i)+ "\n");
		}
		return true;
	}	
	
	/** Method to collect input for rolling dice to determine the order among players*/
	public void promptRollForOrder(int[][] dieResult, int start, int end){
		System.out.println("============================================================");
		System.out.println("Now, roll a die to determine your order in playing the game.\n");
	}
	
	// Method to notify users about getting the same result and prompt for another attempt*/
	public void showRepeat(int[][] dieResult, int start, int end) {
		System.out.println("The following players have the same result of " + dieResult[start][1]+":");
		for (int i=start; i<=end; i++)
			System.out.print("Player " + dieResult[i][0] + "\t");
		System.out.println("\n\nSo, these players have to roll the die again.");
	}
	
	/** Method to print out the players' order in playing the game*/
	public void printOrder (Player[] player, int num) {
		System.out.println("The order of players during the game is:");
		for (int i=0; i<num-1; i++) {
			System.out.print("Player " + player[i].getPlayerID() + " -> ");
		}
		System.out.print("Player " + player[num-1].getPlayerID()+ "\n");
		System.out.println("======================================================================\n");
	}
	
	/** Method to prompt user to pick a role card and display the result*/
	public void pickRole (int numOfPlayer, Player[] player, int[] IDList) {
		PlayerDataProcessor pdp = new PlayerDataProcessor();
		String role;
		
		System.out.println("Now, every player have to pick a role card.");
		
		/**to require the user to press enter to pick*/
		for (int i=0; i<numOfPlayer; i++) {
			System.out.print("Player " + player[i].getPlayerID());
			System.out.println(", press enter to pick a card...");
			
			Scanner keyboard = new Scanner(System.in);
			keyboard.nextLine();
			
			role=pdp.updateRole(player[i],i,IDList);
			System.out.println("The role card you pick is: " + role);
			System.out.println("------------------------------------------");
		}
	}
	
	/**call PlayerDataProcessor to update the SE value and display the updated status*/
	public void updateSEValue(Player player){
		System.out.println("\nSE Points Changed!");
		printSinglePlayerInfo(player);
	}
}
